package com.golxen.framework.event.condition.dao;


import com.golxen.framework.db.DBManager;
import com.golxen.framework.db.dao.AbstractDaoImplByEntity;
import com.golxen.framework.event.condition.dao.entity.UserConditionData;
import com.golxen.utils.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserConditionDaoImpl extends AbstractDaoImplByEntity<UserConditionData> {

	public Map<Integer, UserConditionData> getUserCondition(long userId) {
		ResultSet rs = null;
		CallableStatement call = null;
		Map<Integer, UserConditionData> dataMap = new ConcurrentHashMap<>();
		String sql = "select * from t_u_task where userId=?";
		try {
			getDb().setConnName(DBManager.userDbName);
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserConditionData data = parse(rs);
				dataMap.put(data.getConditionId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	@Override
	protected boolean addData(UserConditionData data) {
		PreparedStatement call = null;
		String sql = "insert into t_u_task(userId,conditionId,`value`,param,type) values(?,?,?,?,?)";
		try {
			getDb().setConnName(DBManager.userDbName);
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getUserId());
			call.setInt(i++, data.getConditionId());
			call.setString(i++, data.getValue().toString());
			call.setString(i++, data.getParam());
			call.setInt(i++, data.getType());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(UserConditionData data) {
		PreparedStatement call = null;
		String sql = "update t_u_task set `value`=?, param=? where userId=? ";
		try {
			getDb().setConnName(DBManager.userDbName);
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setString(i++, data.getValue().toString());
			call.setString(i++, data.getParam());
			call.setLong(i++, data.getUserId());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}



	@Override
	protected UserConditionData parseData(ResultSet rs) throws SQLException {
		UserConditionData data = new UserConditionData();
		data.setUserId(rs.getLong("userId"));
		String value = rs.getString("value");
		if (!StringUtils.isNullOrEmpty(value)) {
			data.setValue(new BigInteger(value));
		}
		data.setConditionId(rs.getInt("conditionId"));
		data.setType(rs.getInt("type"));
		data.setParam(rs.getString("param"));
		return data;
	}
}
